package com.itheima.health.controller;

import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 检查组管理
 */
@RestController
@RequestMapping("/checkgroup")
@Slf4j
public class CheckGroupController {
    @Autowired
    private CheckGroupService checkGroupService;

    /**
     * 新增检查组
     * @param checkitemIds
     * @param checkGroup
     * @return
     */
    @RequestMapping("/add.do")
    public Result add(Integer[] checkitemIds, @RequestBody CheckGroup checkGroup){
        log.info("添加检查组:{}",checkGroup);
        try {
            checkGroupService.add(checkitemIds,checkGroup);
            return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询检查组:{}",queryPageBean);
        PageResult pageResult = checkGroupService.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * 根据id查询检查组
     * @param id
     * @return
     */
    @RequestMapping("/findById.do")
    public Result findById(Integer id){
        log.info("根据id查询检查组:{}",id);
        //1.调用业务层查询
        CheckGroup checkGroup = checkGroupService.findById(id);
        if(checkGroup != null){
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
        }
        return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
    }

    /**
     * 编辑检查组
     * @param checkitemIds
     * @param checkGroup
     * @return
     */
    @RequestMapping("/edit.do")
    public Result edit(Integer[] checkitemIds, @RequestBody CheckGroup checkGroup){
        log.info("编辑检查组:{}",checkGroup);
        try {
            checkGroupService.edit(checkitemIds,checkGroup);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }


    /**
     * 处理删除请求的控制器方法
     * 该方法用于删除指定的检查组
     *
     * @param id 检查组的ID，用于标识要删除的检查组
     * @return 返回一个Result对象，表示删除操作的结果
     */
    @RequestMapping("/delete.do")
    public Result delete(Integer id) {
        //1. 调用业务层
        Boolean flag = checkGroupService.delete(id);
        // 根据删除操作的结果，返回相应的Result对象
        if (flag) {
            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }
        return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
    }

    @RequestMapping("/findAll.do")
    public Result findAll(){
        try {
            //1.调用业务层
            List<CheckGroup> list = checkGroupService.findAll();
            //2.返回结果
            if (list != null && list.size() > 0) {
                return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
            }
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            //3.返回结果
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/findCheckGroupIdsBySetMealId.do")
    public Result findCheckGroupIdsBySetMealId(Integer setMealId){
        //1.调用业务层
        Integer[] checkGroupIds = checkGroupService.findCheckGroupIdsBySetMealId(setMealId);
        //2.返回结果
        if (checkGroupIds != null && checkGroupIds.length > 0) {
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupIds);
        }
        return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
    }

}
